namespace('is.outlet', new function(){
    var prefix = 'outlet_';

    // Helper function that performs different actions based on event method
    this.action = function(targets) {
        return function(event) {
            var dict = targets;
            if(dict.hasOwnProperty(event.data.method)) {
                event.preventDefault();
                return dict[event.data.method](event);
            }
            return false;
        };
    };

    // Returns outlet standard notation for outlet name
    this.name = function(outlet) {
        return prefix + outlet;
    };

    // Registration helper that binds the outlet to the target set
    this.register = function(outlet, targets) {
        is.register(is.outlet.name(outlet), is.outlet.action(targets));
    };

    this.reapply = function(region) {
        $(region.selector + ' .outlet').each(function(){
            var name = prefix + $(this).attr('outlet');

            // Create the cache for the outlet
            if(typeof(is.cache[name]) == 'undefined') is.cache[name] = {};
            is[name] = is.run_method(name);
            is[name + '_all'] = is.run_method_all(name);

            // Bind all supported actions to the cache
            var methods = $(this).attr('methods').split(',');
            for(method in methods) {
                var data = { 'method' : methods[method], 'target': $(this)};

                // Bind the action to the outlet
                $(this).bind(methods[method], data, function(event){
                    return is[name + '_all'](event);
                });
            }
        });
    };

    // Initializes the outlet system by identifying and registering outlets
    // found inside the document
    this.init = function() {
        prefix = (typeof(prefix) != 'undefined') ? prefix : '';
        $('.outlet').each(function(){
            var name = prefix + $(this).attr('outlet');

            // Create the cache for the outlet
            is.cache[name] = {};
            is[name] = is.run_method(name);
            is[name + '_all'] = is.run_method_all(name);

            // Bind all supported actions to the cache
            var methods = $(this).attr('methods').split(',');
            for(method in methods) {
                var data = { 'method' : methods[method], 'target': $(this)};

                // Bind the action to the outlet
                $(this).bind(methods[method], data, function(event){
                    return is[name + '_all'](event);
                });
            }
        });
    };

    // Bind this function to the page startup
    is.register('init',    this);
    is.register('reapply', this);
});
